{% extends "base.html" %}
{% load	bootstrap3 %}
{% load i18n %}
{% load static %}
{% block header-css %}



{% endblock %}

{% block  title %}{% trans 'UserInfo' %}{% endblock %}

{% block page-content %}
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>{% trans 'UserInfo' %}</h2>
            <ol class="breadcrumb">
                <li>
                     <a href="/">{% trans 'Home' %}</a>
                </li>
                <li>
                    <a>{% trans 'System Setup' %}</a>
                </li>
                <li class="active">
                    <strong>{% trans 'UserInfo' %}</strong>
                </li>
            </ol>
        </div>
        <div class="col-lg-2">

        </div>
    </div>
    <div class="wrapper wrapper-content animated fadeInRight   ">
        <div class="row">

            <div class="col-lg-12" id="split-right">

                <div class="ibox float-e-margins">

                    <div class="ibox-title">
                        <h5>{% trans 'UserInfo' %}</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="#" >{% trans 'Config Option 1' %}</a>
                                </li>
                                <li><a href="#" >{% trans 'Config Option 2' %}</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="table-responsive">
                            <form id="cha" class="form-horizontal" action="" method="GET">
                                {% csrf_token %}
                                <div class="pull-left"><input type="text" class="form-control" name="name" placeholder="{% trans 'Username' %}" required></div>
                                &nbsp;
                                <button class="btn btn-sm btn-primary" type="submit">{% trans 'Query' %}</button>

                                <a href="{% url 'users:users_add' %}" class="btn btn-sm btn-primary ">{% trans 'Append' %}</a>

                                <a class="btn btn-sm btn-danger  asset_all_del">{% trans 'Batch Remove' %}</a>

                                <a class="btn btn-sm btn-warning asset_all_cp">{% trans 'Change Password' %}</a>

                            </form>
                            <form id="del_form_asset_all" class="form-horizontal"
                                  action="" method="post">
{#                                {% csrf_token %}#}
                                <table class="table table-striped table-bordered table-hover dataTables-asset">
                                    <thead>
                                    <tr>
                                       <th width="20">
                                            <input type="checkbox" id="CheckedAll">
                                        </th>
                                        <th>{% trans 'UserName' %}</th>
                                        <th>{% trans 'Email Address' %}</th>
                                        <th>{% trans 'User Department' %}</th>
                                        <th>{% trans 'Project Name' %}</th>
                                        <th>{% trans 'User Type' %}</th>
                                        <th>{% trans 'User Role' %}</th>
                                        <th>{% trans 'Login Date Time' %}</th>
                                        <th>{% trans 'Status' %}</th>
                                        <th>{% trans 'Action' %}</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for   row   in   user_list.object_list %}
                                        <tr class="gradeA" id="{{ row.id }}">
                                        <td><input type="checkbox" value="{{ row.id }}" name="id"></td>
                                        <td><a href="{% url  "users:users_detail"   pk=row.id %}">{{ row.username }}</a>
                                        </td>

                                        <td>{{ row.email }}</td>
                                        <td>{{ row.department }}</td>
                                        <td class="center">{% for i in row.project.all %}{{ i.name }} {% endfor %}</td>
                                        <td>{% if row.is_superuser == 1 %}{% trans 'Superuser' %}{% else %}{% trans 'Username' %}{% endif %}</td>

                                        <td>{{ row.role }}</td>
                                        <td>{{ row.login_date|date:"Y-m-d H:i:s" }}</td>
                                        <td>{% ifequal  row.is_active  1 %} <span class="fa fa-check-circle" style="color: green"></span>{% else %} <span class="fa fa-times-circle" style="color: red"></span>{% endifequal %}</td>
                                        <td class="center">
                                            <a class="btn btn-primary  btn-xs " href="{% url 'users:users_update' pk=row.id %}">{% trans 'Update' %}</a>
                                            <a class="btn btn-danger  btn-xs asset_del">{% trans 'Delete' %}</a>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                    </tbody>

                                </table>
                                <div class="pull-right">
                                     <nav class="pagination">
                                        {% if user_list.has_previous %}
                                            <li class="long"><a href="?{{ user_list.previous_page_number.querystring }}">{% trans 'Home' %}</a></li>
                                        {% else %}
                                            <li><a href="{% url "users:users_list" %}?page=1">{% trans 'Home' %}</a></li>
                                        {% endif %}

                                        {% for page in user_list.pages %}
                                        {% if page %}
                                            {% ifequal page user_list.number %}
                                                <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
                                            {% else %}
                                                 <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li>
                                            {% endifequal %}
                                        {% else %}
                                            <li class="none"><a href="">...</a></li>
                                        {% endif %}
                                        {% endfor %}
                                        {% if user_list.has_next %}
                                            <li class="long"><a href="?{{ user_list.next_page_number.querystring }}">{% trans 'Next' %}</a></li>
                                        {% endif %}
                                        <li>
                                            <a href="{% url "users:users_list" %}?page={{ user_list.paginator.num_pages }}">{% trans 'Trailer' %}</a>
                                        </li>
                                        <li><span style="color: #0a0a0a">{% trans 'Total' %}: &nbsp;{{ user_list.paginator.num_pages }} {% trans 'Page' %}</span>
                                        </li>
                                    </nav>
                                </div> <!--页码 -->
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>



{% endblock %}
{% block footer-js %}


    <script>
        $(document).on('click', '.asset_del', function () {
            var id = $(this).parent().parent().attr('id');
            swal({
                title: '{% trans 'You Are Sure To Delete' %}',
                text: '{% trans 'You Will Never Recover' %}',
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: '{% trans 'confirm' %}',
                cancelButtonText:'{% trans 'cancel' %}',
                closeOnConfirm: false
            }, function () {
                $.ajax({
                    headers: {"X-CSRFToken": '{{ csrf_token }}'},
                    url: "{% url 'users:users_all_del' %}",
                    type: 'POST',
                    data: {'nid': id},
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.status) {
                            swal({title: '{% trans 'Delete' %}', text: '{% trans 'Deleted Successfully' %}', type: "success"}, function () {
                                window.location.reload();
                            })
                        } else {
                            swal('{% trans 'Delete Error' %}', '{% trans 'Delete Error' %}' + "[ " + obj.error + " ]" + '{% trans 'Error' %}', "error");
                        }
                    }
                });


            });

        });

        $(document).on('click', '.asset_all_del', function () {
            swal({
                title: '{% trans 'You Are Sure To Delete' %}',
                text: '{% trans 'You Will Never Recover' %}',
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: '{% trans 'confirm' %}',
                cancelButtonText:'{% trans 'cancel' %}',
                closeOnConfirm: false
            }, function () {
                $.ajax({
                    headers: {"X-CSRFToken": '{{ csrf_token }}'},
                    url: "{% url 'users:users_all_del' %}",
                    type: 'POST',
                    data: $('#del_form_asset_all').serialize(),
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.status) {
                            swal({title: '{% trans 'Delete' %}', text: '{% trans 'Deleted Successfully' %}', type: "success"}, function () {
                                window.location.reload();
                            })
                        } else {
                            swal('{% trans 'Delete Error' %}', '{% trans 'Delete Error' %}' + "[ " + obj.error + " ]" + '{% trans 'Error' %}', "error");
                        }
                    }
                });


            });
        });

        $(document).on('click', '.asset_all_cp', function () {
            var asset_id = document.getElementsByName('id');
            asset_id_list = [];
            //console.log(asset_id);
            for (var i = 0; i < asset_id.length; i++) {
                if (asset_id[i].checked) {
                    asset_id_list.push(asset_id[i].value);
                }
            }

            if ( asset_id_list.length != 1 ) {
                swal('{% trans 'Change Password Error' %}', '{% trans 'Your must choose only one user' %}', "error");
            }else{

                swal({
                    title: '{% trans 'Please enter a new password' %}',
                    text: '<div class="col-sm-12"><input style="display:block" type="password" value="" id="password1" name="password1" placeholder="{% trans 'Input Password' %}"></div> \
                        <div class="col-sm-12"><input style="display:block" type="password" value="" id="password2" placeholder="{% trans 'Input Password Too' %}" name="password2"></div>',
                    html: true,
                    type: "info",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: '{% trans 'confirm' %}',
                    cancelButtonText:'{% trans 'cancel' %}',
                    closeOnConfirm: false
                }, function () {
                    var password1 = document.getElementById('password1').value;
                    var password2 = document.getElementById('password2').value;

                    if ( password1 == password2 && password1.length >= 6 ) {
                        $.ajax({
                            headers: {"X-CSRFToken": '{{ csrf_token }}'},
                            url: "{% url 'users:users_change_password' %}",
                            type: 'POST',
                            data: {'id': asset_id_list[0],'password':password1},
                            success: function (data) {
                               var obj = JSON.parse(data);
                               if (obj.status) {
                                   swal({title: '{% trans 'Change Password' %}', text: '{% trans 'Change Password Successfully' %}', type: "success"}, function () {
                                   })
                               } else {
                                   swal('{% trans 'Change Password Error' %}', '{% trans 'Delete Error' %}' + "[ " + obj.error + " ]" + '{% trans 'Error' %}', "error");
                               }
                            }
                         });
                    }else{
                        swal('{% trans 'Change Password Error' %}', '{% trans 'You must select a user and the password length is greater than or equal to 6!' %}', "error");
                    }
                });
            }
        });

    </script>

    {% endblock %}

{% block footer-js-ajax %}
{% endblock %}